#include "movielabel.h"

#include <QPainter>
#include <QTextDocument>
#include <QAbstractTextDocumentLayout>
#include <QStringList>
#include <QFont>
#include <QPalette>

#include <KLocale>

#include <kdclib.h>

MovieLabel::MovieLabel( QWidget *aParent ) : QWidget( aParent ), fMovie( NULL )
{

}

void MovieLabel::setMovie( KDCMovie *aMovie )
{
	if ( aMovie )
		resize( qRound( 100 + ( ( aMovie ) ? aMovie->audio().size() : 0 ) * 110 ), 155 );
    setMinimumSize( width(), height() );
    setMaximumSize( width(), height() );
	fMovie = aMovie;
}

void MovieLabel::paintEvent( QPaintEvent *aEvent )
{
    QPainter painter( this );
    /*
	painter.save();
    QColor color = Qt::white;//QColor( 0xff, 0xff, 0xe1 );
    painter.setBrush( color );
    painter.setPen( color );
    painter.drawRect( rect() );
    painter.restore();
	*/
	if ( !fMovie )
		return;
    QTextDocument doc;
    QColor color = Qt::black;//palette().color( QPalette::HighlightedText );
	QFont f;
	f.setPointSizeF( 7.5 );
    doc.setDefaultFont( f );
	uint i;
    QString str;
    str.append( "<table><td rowspan=""7"">" );
    str.append( "</td>" );
    str.append( "<th colspan=""2""><font size=""+1"" color=""#366CA2""><b>Video</b></font></th><hr>" ); //video
    for ( i=0; i<fMovie->audio().size(); ++i )
    {
        str.append( "<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>" );
        str.append( "<th colspan=""2""><font size=""+1"" color=""#366CA2""><b>Audio</b></th></font><hr>" ); //audio
    }
    str.append( "<tr>" );
    str.append( "<td><font color=""#777777"">" + i18n("System") + ": </font></td>" );
    str.append( "<td><font color=""" + color.name() + """>" + toString( fMovie->videoFormat() ) + "</font></td>" ); //video
    for ( i=0; i<fMovie->audio().size(); ++i )
    {
        str.append( "<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>" );
        str.append( "<td><font color=""#777777"">" + i18n("Codec") + ": </font></td>" );
        str.append( "<td><font color=""" + color.name() + """>" + toString( fMovie->audio().at( i )->codec() ) + "</font></td>" ); //audio
    }
    str.append( "<tr>" );
    str.append( "<td><font color=""#777777"">" + i18n("Fps") + ": </font></td>" );
    str.append( "<td><font color=""" + color.name() + """>" + QString::number( fMovie->fps(), 'f', 3 ) + "</font></td>" ); //video
    for ( i=0; i<fMovie->audio().size(); ++i )
    {
        str.append( "<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>" );
        str.append( "<td><font color=""#777777"">" + i18n("Channels") + ": </font></td>" );
        str.append( "<td><font color=""" + color.name() + """>" + QString::number( fMovie->audio().at( i )->channelCount() ) + "</font></td>" ); //audio
    }
    str.append( "<tr>" );
    str.append( "<td><font color=""#777777"">" + i18n("Bitrate") + ": </font></td>" );
    str.append(" <td><font color=""" + color.name() + """>" + bitrateAsString( fMovie->video()->bitrate() ) + " </font></td>" ); //video
    for ( i=0; i<fMovie->audio().size(); ++i )
    {
        str.append( "<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>" );
        str.append( "<td><font color=""#777777"">" + i18n("Bitrate") + ": </font></td>" );
        str.append( "<td><font color=""" + color.name() + """>" + bitrateAsString( fMovie->audio().at( i )->bitrate() ) + " </font></td>" ); //audio
    }
    str.append( "<tr>" );
    str.append( "<td><font color=""#777777"">" + i18n("Resolution") + ": </font></td>" );
    str.append( "<td><font color=""" + color.name() + """>" + QString( "%1x%2" ).arg( fMovie->video()->width() ).arg( fMovie->video()->height() ) + "</font></td>" ); //video
    for ( i=0; i<fMovie->audio().size(); ++i )
    {
        str.append( "<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>" );
        str.append( "<td><font color=""#777777"">" + i18n("Id") + ": </font></td>" );
        str.append( "<td><font color=""" + color.name() + """>" + QString::number( fMovie->audio().at( i )->metaData()->id() ) + " </font></td>" ); //audio
    }
    str.append( "<tr>" );
	str.append( "<td><font color=""#777777"">" + i18n("Ratio") + ": </font></td>" );
    str.append( "<td><font color=""" + color.name() + """>" + toString( fMovie->video()->aspectRatio() ) + "</font></td></table>" ); //video
	
    doc.setHtml( str );
	QAbstractTextDocumentLayout::PaintContext context;
    QRect rc = rect();
	doc.setPageSize( rc.size() );
	doc.documentLayout()->draw( &painter, context );
}
